﻿/*****************************************
 * 
 *  copyright (c) by ylshen
 * 
 *  author : ylshen (syl.lucky@gmail.com)
 *  
 **************************************** */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JYStockAnalyser.Model;
using JYStockAnalyser.Face;
using JYStockAnalyser.ClientLib;
using ZedGraph;

namespace JYStockAnalyser.WinUI
{
    /// <summary>
    /// 个股财务指标分析界面
    /// </summary>
    public partial class FrmFinancialAnalyse :  FrmFunctionBase
    {
        Button m_gLGroup1SelectedButton;
        Button m_gLGroup2SelectedButton;
        Button m_gLGroup3SelectedButton;

        bool m_searched = false;

        //-----当前查询条件------------------------------------------
        string stockNO;
        GuideLineType selectedGuideLineType;
        YearQuarter beginYearQuarter;
        YearQuarter endYearQuarter;
        //---------------------------------------------------------------------


        public FrmFinancialAnalyse()
        {
            InitializeComponent();          
        }

        private void FrmFinancialAnalyse_Load(object sender, EventArgs e)
        {
            m_init();
           // m_doSearch();
        }

        /// <summary>
        /// 初始化界面
        /// </summary>
        private void m_init()
        {
            if (!this.DesignMode) { this.stockinputer.BindData(); }

            foreach (string report in ClientCache.Instance.FinancialCategories)
            {
                this.ddlReports.Items.Add(report);
            }
            this.ddlReports.SelectedIndex = 0;

            int[][] defaultGuideLines = ClientCache.Instance.DefaultFinancialAnalyseGuideLines;

            GuideLineType gl1_1 = ClientCache.Instance.GetGuideLineType(defaultGuideLines[0][0]);
            this.btnGL1_1.Text = gl1_1.Name;
            this.btnGL1_1.Tag = gl1_1;

            GuideLineType gl1_2 = ClientCache.Instance.GetGuideLineType(defaultGuideLines[0][1]);
            this.btnGL1_2.Text = gl1_2.Name;
            this.btnGL1_2.Tag = gl1_2;

            GuideLineType gl1_3 = ClientCache.Instance.GetGuideLineType(defaultGuideLines[0][2]);
            this.btnGL1_3.Text = gl1_3.Name;
            this.btnGL1_3.Tag = gl1_3;

            GuideLineType gl1_4 = ClientCache.Instance.GetGuideLineType(defaultGuideLines[0][3]);
            this.btnGL1_4.Text = gl1_4.Name;
            this.btnGL1_4.Tag = gl1_4;

            GuideLineType gl1_5 = ClientCache.Instance.GetGuideLineType(defaultGuideLines[0][4]);
            this.btnGL1_5.Text = gl1_5.Name;
            this.btnGL1_5.Tag = gl1_5;

            GuideLineType gl2_1 = ClientCache.Instance.GetGuideLineType(defaultGuideLines[1][0]);
            this.btnGL2_1.Text = gl2_1.Name;
            this.btnGL2_1.Tag = gl2_1;

            GuideLineType gl2_2 = ClientCache.Instance.GetGuideLineType(defaultGuideLines[1][1]);
            this.btnGL2_2.Text = gl2_2.Name;
            this.btnGL2_2.Tag = gl2_2;

            GuideLineType gl2_3 = ClientCache.Instance.GetGuideLineType(defaultGuideLines[1][2]);
            this.btnGL2_3.Text = gl2_3.Name;
            this.btnGL2_3.Tag = gl2_3;


            GuideLineType gl3_1 = ClientCache.Instance.GetGuideLineType(defaultGuideLines[2][0]);
            this.btnGL3_1.Text = gl3_1.Name;
            this.btnGL3_1.Tag = gl3_1;

            GuideLineType gl3_2 = ClientCache.Instance.GetGuideLineType(defaultGuideLines[2][1]);
            this.btnGL3_2.Text = gl3_2.Name;
            this.btnGL3_2.Tag = gl3_2;

            GuideLineType gl3_3 = ClientCache.Instance.GetGuideLineType(defaultGuideLines[2][2]);
            this.btnGL3_3.Text = gl3_3.Name;
            this.btnGL3_3.Tag = gl3_3;


            StyleManager.SetStyle(this.zgc);
            StyleManager.SetStyle(this.zgc2);
            StyleManager.SetStyle(this.zgc3);

            StyleManager.SetGrowthGridStyle(this.dgvGrowth);

            this.stockinputer.Focus();
            selectedGuideLineType = (GuideLineType)this.ddlGuideLines.SelectedItem;
        }

        private void ddlReports_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_bindSelectedReportGuideLines();
        }

        private void m_bindSelectedReportGuideLines()
        {
            this.ddlGuideLines.Items.Clear();
            this.ddlGuideLines.Items.Add("-请选择-");
            if (this.ddlReports.SelectedIndex == -1)
                return;
            List<GuideLineType> types = ClientCache.Instance.AllFinancialGuideLineTypes[(string)this.ddlReports.SelectedItem][1];

            foreach (GuideLineType type in types)
            {
                this.ddlGuideLines.Items.Add(type);
            }
            this.ddlGuideLines.SelectedIndex = 1;
        }

        private void btnUPStockNO_Click(object sender, EventArgs e)
        {

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            selectedGuideLineType = (GuideLineType)this.ddlGuideLines.SelectedItem;
            m_doSearch();
        }

        private void m_doSearch()
        {
            if (this.stockinputer.SelectedStockNO == "")
            {
                Tool.ShowInformationBox("请输入股票代码!");
                this.stockinputer.Focus();
                return;
            }

            if (this.ddlGuideLines.SelectedIndex == -1 || this.ddlGuideLines.SelectedIndex == 0)
            {
                Tool.ShowInformationBox("请选择一个指标!");
                this.ddlGuideLines.Focus();
                return;
            }

            stockNO = this.stockinputer.SelectedStockNO;
           
            beginYearQuarter = this.periodInputer1.BeginQuarter;
            endYearQuarter = this.periodInputer1.EndQuarter;
          

            m_checkSelectGuideLines();

            GuideLineType[] searchingTypes = new GuideLineType[] { selectedGuideLineType, (GuideLineType)m_gLGroup2SelectedButton.Tag, (GuideLineType)m_gLGroup3SelectedButton.Tag };

            object[] objs = new StockManageFace().SearchGuideLines(stockNO, "年", beginYearQuarter, endYearQuarter, searchingTypes);

            Stock stock = (Stock)objs[0];

            if (stock == null)
            {
                Tool.ShowInformationBox("没有此股票代码!");
                this.stockinputer.Focus();
                return;
            }



            List<GuideLine> lines1 = (List<GuideLine>)objs[2];
            GuideLinesCollator.CollateLines("年", beginYearQuarter, endYearQuarter, searchingTypes[0], lines1);

            List<GuideLine> lines2 = (List<GuideLine>)objs[3];
            GuideLinesCollator.CollateLines("年", beginYearQuarter, endYearQuarter, searchingTypes[1], lines2);

            List<GuideLine> lines3 = (List<GuideLine>)objs[4];
            GuideLinesCollator.CollateLines("年", beginYearQuarter, endYearQuarter, searchingTypes[2], lines3);

            this.lblStockNO.Text = stock.NO;
            this.lblStockName.Text = stock.Name;
            this.plStock.Tag = stock;

            m_bindZGC(this.zgc, lines1, searchingTypes[0]);

            m_bindZGC(this.zgc2, lines2, searchingTypes[1]);

            m_bindZGC(this.zgc3, lines3, searchingTypes[2]);

            List<GrowthStatItem> items = GrowthStatItemBuilder.Builditems(lines1);
            this.dgvGrowth.DataSource = items;
            StyleManager.SetGrowthGridCols(this.dgvGrowth, "年", selectedGuideLineType);

            m_searched = true;
        }


        private void m_bindZGC(ZedGraphControl theZgc, List<GuideLine> lines, GuideLineType type)
        {
            GraphPane myPane = theZgc.GraphPane;

            List<string> strs = new List<string>();
            List<double> values = new List<double>();

            foreach (GuideLine line in lines)
            {
                strs.Add(line.GetTimeSimpleString());
                values.Add((double)line.Value);
            }

            myPane.XAxis.Type = AxisType.Text;
            myPane.XAxis.Scale.TextLabels = strs.ToArray();

            myPane.CurveList.Clear();
            LineItem myCurve = myPane.AddCurve(type.GetFullName(), null, values.ToArray(), Color.Yellow);
            StyleManager.SetCommonLineItemStyle(myCurve);

            theZgc.AxisChange();
            theZgc.Refresh();
        }

        private void ddlStockTypes_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        /// <summary>
        /// 第一分析组某指标按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGL1_1_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            m_setGuideGroup1SelectButton(btn);

            if (m_searched)
            {
                GuideLineType type = (GuideLineType)btn.Tag;
                selectedGuideLineType = type;
                object[] objs = new StockManageFace().SearchGuideLines(stockNO, "年", beginYearQuarter, endYearQuarter, type);

                List<GuideLine> lines = (List<GuideLine>)objs[1];
                GuideLinesCollator.CollateLines("年", beginYearQuarter, endYearQuarter, type, lines);

                m_bindZGC(this.zgc, lines, type);
            }
        }

        /// <summary>
        /// 第二分析组某指标按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGL2_1_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            m_setGuideGroup2SelectButton(btn);

            if (m_searched)
            {
                GuideLineType type = (GuideLineType)btn.Tag;
                object[] objs = new StockManageFace().SearchGuideLines(stockNO, "年", beginYearQuarter, endYearQuarter, type);

                List<GuideLine> lines = (List<GuideLine>)objs[1];
                GuideLinesCollator.CollateLines("年", beginYearQuarter, endYearQuarter, type, lines);

                m_bindZGC(this.zgc2, lines, type);
            }
        }

        /// <summary>
        /// 第三分析组某指标按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGL3_1_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            m_setGuideGroup3SelectButton(btn);

            if (m_searched)
            {
                GuideLineType type = (GuideLineType)btn.Tag;
                object[] objs = new StockManageFace().SearchGuideLines(stockNO, "年", beginYearQuarter, endYearQuarter, type);

                List<GuideLine> lines = (List<GuideLine>)objs[1];
                GuideLinesCollator.CollateLines("年", beginYearQuarter, endYearQuarter, type, lines);

                m_bindZGC(this.zgc3, lines, type);
            }
        }

        private void btnGL1_1_MouseEnter(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            toolTip1.SetToolTip(btn, ((GuideLineType)btn.Tag).Name);
            //((Button)sender).show
        }


        private void btnGL1_1_MouseLeave(object sender, EventArgs e)
        {

        }


        private void m_setGuideGroup1SelectButton(Button btn)
        {
            if (m_gLGroup1SelectedButton == btn)
                return;

            btn.BackColor = Color.SteelBlue;
            btn.ForeColor = Color.White;

            if (this.m_gLGroup1SelectedButton != null)
            {
                this.m_gLGroup1SelectedButton.BackColor = Color.AliceBlue;
                this.m_gLGroup1SelectedButton.ForeColor = Color.Black;
            }

            this.m_gLGroup1SelectedButton = btn;
        }

        private void m_setGuideGroup2SelectButton(Button btn)
        {
            if (m_gLGroup2SelectedButton == btn)
                return;

            btn.BackColor = Color.SteelBlue;
            btn.ForeColor = Color.White;

            if (this.m_gLGroup2SelectedButton != null)
            {
                this.m_gLGroup2SelectedButton.BackColor = Color.AliceBlue;
                this.m_gLGroup2SelectedButton.ForeColor = Color.Black;
            }

            this.m_gLGroup2SelectedButton = btn;
        }

        private void m_setGuideGroup3SelectButton(Button btn)
        {
            if (m_gLGroup3SelectedButton == btn)
                return;

            btn.BackColor = Color.SteelBlue;
            btn.ForeColor = Color.White;

            if (this.m_gLGroup3SelectedButton != null)
            {
                this.m_gLGroup3SelectedButton.BackColor = Color.AliceBlue;
                this.m_gLGroup3SelectedButton.ForeColor = Color.Black;
            }

            this.m_gLGroup3SelectedButton = btn;
        }

        /// <summary>
        /// 检查是否三个分析图的指标都已经选择，若没有选择，则选择第一个指标
        /// </summary>
        private void m_checkSelectGuideLines()
        {
            if (this.m_gLGroup1SelectedButton == null)
            {
                m_setGuideGroup1SelectButton(btnGL1_1);
            }

            if (this.m_gLGroup2SelectedButton == null)
            {
                m_setGuideGroup2SelectButton(btnGL2_1);
            }

            if (this.m_gLGroup3SelectedButton == null)
            {
                m_setGuideGroup3SelectButton(btnGL3_1);
            }
        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void plStock_MouseEnter(object sender, EventArgs e)
        {
            if (this.plStock.Tag != null)
            {
                Stock stock = (Stock)this.plStock.Tag;
                toolTip1.SetToolTip(this.plStock, String.Format("股票代码：{0}\r\n股票名称：{1}\r\n公司名称：{2}\r\n行业：{3}\r\n省份：{4}\r\n地址：{5}\r\n上市地点：{6}", stock.NO, stock.Name, stock.CompanyName, stock.Trade, stock.Province, stock.address, stock.Location));
            }
        }

        private void stockinputer_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

        private void btnFindStockByTrade_Click(object sender, EventArgs e)
        {
            FrmSelectStocks frm = new FrmSelectStocks();
            frm.StockSelected += new FrmSelectStocks.StockSelectedHandler(frm_StockSelected);
            frm.Show();
        }

        void frm_StockSelected(Stock stock)
        {
            this.stockinputer.SelectedItem = stock;
            m_doSearch();
        }
    }
}
